package com.marklogic.ps.servicetester.gui;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.marklogic.ps.servicetester.domain.ServerConnectionInfo;

public class ServerConnectionDialog extends JDialog {
	
	private ServerConnectionInfo serverConnectionInfo;
	JTextField server;
	JTextField user;
	JTextField password;
	
	public ServerConnectionDialog(Frame parent, final ActionListener saveListener) {
		super(parent, "Add Database Connection", true);
		serverConnectionInfo = new ServerConnectionInfo("http://localhost:9001/", "admin", "admin");
		
		
		Container container = getContentPane();
		
		GridBagConstraints c = new GridBagConstraints();
		GridBagLayout gbl = new GridBagLayout();
		JPanel contentPanel = new JPanel();
		contentPanel.setLayout(gbl);
		
		c.fill = GridBagConstraints.BOTH;
		c.insets = new Insets(4,4,4,4);
		c.gridy = 0;
		c.weighty = 0;
		
		server = new JTextField(serverConnectionInfo.getBaseUrl());
		addField(c, contentPanel, server, "Server Base URI");
		
		user = new JTextField(serverConnectionInfo.getUsername());
		addField(c, contentPanel, user, "Username");
		
		password = new JTextField(serverConnectionInfo.getPassword());
		addField(c, contentPanel, password, "Password");
		
		JButton okButton = new JButton("OK");
		okButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setDataFields();
				e.setSource(serverConnectionInfo);
				saveListener.actionPerformed(e);
				closeAndSave();
			}});
		JButton cancelButton = new JButton("Cancel");
		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				closeAndSave();
			}});
		
		JPanel buttonPanel = new JPanel();
		buttonPanel.add(okButton);
		buttonPanel.add(cancelButton);
		
		container.add(contentPanel, BorderLayout.CENTER);
		container.add(buttonPanel, BorderLayout.SOUTH);
		
		//contentPanel.setMinimumSize(new Dimension(100, 100));
		//setSize(contentPanel.getPreferredSize());
		setSize(350, 200);
	}
	
	private void setDataFields() {
		serverConnectionInfo.setBaseUrl(server.getText());
		serverConnectionInfo.setUsername(user.getText());
		serverConnectionInfo.setPassword(password.getText());
	}
	
	private void closeAndSave() {
		setVisible(false);
	}
	
	private void addField(GridBagConstraints c, Container contentPanel, JComponent field, String label) {
		c.gridy++;
		c.gridx = 0;
		c.insets = new Insets(4,4,4,4);
		c.weightx = 0.1;
		c.fill = GridBagConstraints.NONE;
		c.anchor = GridBagConstraints.FIRST_LINE_END;
		contentPanel.add(new JLabel(label), c);
		
		c.gridx = 1;
		c.weightx = 0.6;
		c.fill = GridBagConstraints.BOTH;
		c.anchor = GridBagConstraints.CENTER;
		c.insets = new Insets(4,4,4,15);
		contentPanel.add(field, c);
		
		//new Box.Filler(mi, pf, mx);
		//contentPanel.add(new JComponent() );
		c.weightx = 0.1;
		c.gridx = 2;
		c.fill = GridBagConstraints.NONE;
		contentPanel.add(new JLabel(""), c);
	}

}
